#include "test_common.hpp"
#include "tools/api.hpp"
#include <fstream>

void test_load_yaml()
{
    std::string testDir = test_common::getTestTempDir();
    std::string yamlFile = testDir + "/test.yaml";
    test_common::cleanupTestDir(testDir);
    
    MKDIR(testDir);
    
    // 创建测试YAML文件
    {
        std::ofstream yaml(yamlFile);
        yaml << "int_value: 42\n";
        yaml << "double_value: 3.14\n";
        yaml << "bool_value: true\n";
        yaml << "string_value: \"test string\"\n";
        yaml.close();
    }
    
    LOAD_YAML(yamlFile);
    
    int intVal = 0;
    GET_PARAM("int_value", intVal);
    ASSERT_EQ(42, intVal);
    
    double doubleVal = 0.0;
    GET_PARAM("double_value", doubleVal);
    ASSERT_NEAR(3.14, doubleVal, 0.01);
    
    bool boolVal = false;
    GET_PARAM("bool_value", boolVal);
    ASSERT_TRUE(boolVal);
    
    std::string stringVal;
    GET_PARAM("string_value", stringVal);
    ASSERT_STREQ("test string", stringVal.c_str());
    
    test_common::cleanupTestDir(testDir);
}

void test_param_not_found()
{
    std::string testDir = test_common::getTestTempDir();
    std::string yamlFile = testDir + "/test.yaml";
    test_common::cleanupTestDir(testDir);
    
    MKDIR(testDir);
    
    // 创建空的YAML文件
    {
        std::ofstream yaml(yamlFile);
        yaml << "other_value: 100\n";
        yaml.close();
    }
    
    LOAD_YAML(yamlFile);
    
    int value = 999;  // 默认值
    GET_PARAM("non_existent", value);
    // 参数不存在时，value应该保持原值
    ASSERT_EQ(999, value);
    
    test_common::cleanupTestDir(testDir);
}

int main()
{
    auto& runner = test_common::TestRunner::getInstance();
    runner.reset();
    
    std::cout << "=== Running Param Module Tests ===" << std::endl;
    
    runner.runTest("test_load_yaml", test_load_yaml);
    runner.runTest("test_param_not_found", test_param_not_found);
    
    std::cout << "\n=== Test Results ===" << std::endl;
    std::cout << "Total: " << runner.getTotal() << std::endl;
    std::cout << "Passed: " << runner.getPassed() << std::endl;
    std::cout << "Failed: " << runner.getFailed() << std::endl;
    
    return runner.getFailed() == 0 ? 0 : 1;
}

